//
// Created by Sugars Mac on 2020/5/26.
//

#ifndef LINUXNPHW_RELATION_H
#define LINUXNPHW_RELATION_H

#define PASSLEN 12   // 密码长度
#define NAMELEN 24   // 用户名长度
#define true 1
#define false 0


typedef struct user_tb{
    char password[PASSLEN];
    char username[NAMELEN];
} UserTable_t; // 存储于文件中的注册用户结构

typedef struct TableList{
    UserTable_t * data;
    struct TableList * next;
}TableList_t;

typedef struct user{
    int sockfd;
    char * username;
    int groupid; // 聊天室id
}user_t;

typedef struct room{
    char * roomname; // 房间名
    char * roompass; // 房间密码
}room_t;

typedef struct rela{
    int sockA; // 私聊建立者
    int sockB; // 私聊接收者
    struct rela* next;
}relationMap;

int searchEmptyRoom(room_t* rooms, int size); // 返回空房间的下标
int findRoomByName(room_t* rooms, int size, char* name); // 返回相应房间名的下标

// 文件有关操作
int readFromFile(TableList_t* head); // 构建用户表
int writeToFile(TableList_t* head); // 写回文件
// 查询用户信息表
int insertTable(TableList_t* head, UserTable_t * user);
UserTable_t * findUserByName(TableList_t* head, char * username);
void showTable(TableList_t* head);
void destroyTable(TableList_t* head);

// 私聊关系操作
int compareRelation(relationMap* r1, relationMap* r2);
int findUserSockByName(user_t* users, int len, char* name);

user_t * buildUser(int sockfd, char* name);
relationMap * buildRelation(int u1, int u2);
int addRelation(relationMap* head, relationMap* rel);
int deleteRelation(relationMap* head, relationMap* rel);
int findRelation(relationMap* head, relationMap* rel);
void showRelation(relationMap* head);
void deleteAllRela(relationMap* head);
relationMap * existRelation(relationMap * head, int u);
relationMap * beContacted(relationMap * head, int u);
void deleteRelationBySocket(relationMap * head, int u);

#endif //LINUXNPHW_RELATION_H
